// 防抖函数
export const debounce = (fn, delay = 500, immediate = false) => {
    let timer = null
    let isFirst = true  // 标记是否是第一次调用

    return function (...args) {
        // 如果是第一次调用且需要立即执行
        if (isFirst && immediate) {
            isFirst = false
            fn.apply(this, args)
            return
        }

        if (timer) clearTimeout(timer)
        timer = setTimeout(() => {
            fn.apply(this, args)
            timer = null
        }, delay)
    }
}